﻿using System;
using System.Collections.Generic;
using System.Windows.Automation;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using QuickUIT.Controls;

namespace QuickUIT.Tests
{
    [TestClass]
    public class UIComboBoxTests
    {
        private BaseState _baseState;

        [TestInitialize]
        public void TestSetup()
        {
            _baseState = new BaseState(TestUtilities.GetTestApplicationFileName(), "@Name='QuickUIT Test Application'");
            _baseState.Execute();
        }

        [TestMethod]
        public void ShouldExpandComboBox()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIComboBox comboBox = window.UIComboBox("@AutomationId='comboBox1'");
            var expandCollapsePattern =
                comboBox.Control.GetCurrentPattern(ExpandCollapsePattern.Pattern) as ExpandCollapsePattern;
            Assert.IsNotNull(expandCollapsePattern);
            Assert.AreEqual(ExpandCollapseState.Collapsed, expandCollapsePattern.Current.ExpandCollapseState);
            comboBox.Expand();
            Assert.AreEqual(ExpandCollapseState.Expanded, expandCollapsePattern.Current.ExpandCollapseState);
        }

        [TestMethod]
        public void ShouldCollapseComboBox()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIComboBox comboBox = window.UIComboBox("@AutomationId='comboBox1'");
            var expandCollapsePattern =
                comboBox.Control.GetCurrentPattern(ExpandCollapsePattern.Pattern) as ExpandCollapsePattern;
            Assert.IsNotNull(expandCollapsePattern);
            comboBox.Expand();
            comboBox.Collapse();
            Assert.AreEqual(ExpandCollapseState.Collapsed, expandCollapsePattern.Current.ExpandCollapseState);
        }

        [TestMethod]
        public void ShouldSelectItemByNameFromComboBox()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIComboBox comboBox1 = window.UIComboBox("@AutomationId='comboBox1'");
            comboBox1.SelectItem("Item 2");
            AutomationElement selection1 = comboBox1.GetSelectedItem();
            Assert.AreEqual("Item 2", selection1.Current.Name);

            UIComboBox comboBox2 = window.UIComboBox("@AutomationId='comboBox2'");
            comboBox2.SelectItem("50");
            AutomationElement selection2 = comboBox2.GetSelectedItem();
            Assert.AreEqual("50", selection2.Current.Name);
        }
        
        [TestMethod]
        public void ShouldGetComboBoxValue()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIComboBox comboBox = window.UIComboBox("@AutomationId='comboBox1'");
            Assert.AreEqual(String.Empty, comboBox.Value);
        }

        [TestMethod]
        public void ShouldGetNullWhenGettingValueForUnsupportedComboBox()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIComboBox comboBox = window.UIComboBox("@AutomationId='comboBox2'");
            Assert.IsNull(comboBox.Value);
        }

        [TestMethod]
        public void ShouldSetComboBoxValue()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIComboBox comboBox = window.UIComboBox("@AutomationId='comboBox1'");
            Assert.AreEqual(String.Empty, comboBox.Value);
            comboBox.Value = "Test Item 1";
            Assert.AreEqual("Test Item 1", comboBox.Value);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))] 
        public void ShouldThrowExceptionWhenSettingValueOnUnsupportedComboBox()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIComboBox comboBox = window.UIComboBox("@AutomationId='comboBox2'");
            comboBox.Value = "Invalid Value";
        }

        [TestMethod]
        public void ShouldGetSelectedItem()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIComboBox comboBox = window.UIComboBox("@AutomationId='comboBox1'");
            comboBox.SelectItem("Item 1");
            AutomationElement selectedItem = comboBox.GetSelectedItem();
            Assert.IsNotNull(selectedItem);
            Assert.AreEqual("Item 1", selectedItem.Current.Name);
        }

        [TestMethod]
        public void ShouldGetSelection()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIComboBox comboBox = window.UIComboBox("@AutomationId='comboBox1'");
            comboBox.SelectItem("Item 1");
            var selection = comboBox.GetSelection();
            Assert.AreEqual(1, selection.Length);
            Assert.AreEqual("Item 1", selection[0].Current.Name);
        }

        [TestMethod]
        public void ShouldGetCanSelectMultiple()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIComboBox comboBox = window.UIComboBox("@AutomationId='comboBox1'");
            Assert.IsFalse(comboBox.CanSelectMultiple);
        }

        [TestMethod]
        public void ShouldGetIsSelectionRequired()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIComboBox comboBox = window.UIComboBox("@AutomationId='comboBox1'");
            Assert.IsFalse(comboBox.IsSelectionRequired);
        }

        [TestMethod]
        public void ShouldGetExpandCollapseState()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIComboBox comboBox = window.UIComboBox("@AutomationId='comboBox1'");
            Assert.AreEqual(ExpandCollapseState.Collapsed, comboBox.ExpandCollapseState);
        }

        [TestMethod]
        public void ShouldGetAllComboBoxes()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            List<UIComboBox> comboBoxes = window.UIComboBoxes();
            Assert.AreEqual(2, comboBoxes.Count);
            Assert.AreEqual("comboBox1", comboBoxes[0].AutomationId);
            Assert.AreEqual("comboBox2", comboBoxes[1].AutomationId);
        }

        [TestCleanup]
        public void TestTeardown()
        {
            _baseState.Cleanup();
        }
    }
}